﻿#include "ClassVideoWidget.h"
#include <QDateTime>
#include <QJsonDocument>
#include "QTextCodec"
#include <QTimer>
#include <QFileDialog>

ClassVideoWidget::ClassVideoWidget(QWidget *parent)
    : QWidget(parent)
    , m_player(nullptr)
    , m_thread(nullptr)
{
    setWindowFlags(Qt::FramelessWindowHint);
    ui.setupUi(this);

    QFile file(":/qss/vlcplayer.qss");
    file.open(QFile::ReadOnly);
    QString qss = file.readAll();
    setStyleSheet(qss);

    initPlayer();
}

ClassVideoWidget::~ClassVideoWidget()
{
    if (m_player)
    {
        m_player->Release();
    }
    if (m_thread)
    {
        m_thread->quit();
        m_thread->wait();
    }
}

void ClassVideoWidget::initPlayer()
{
    m_player = new VlcPlayer(this);
    m_thread = new QThread(this);
    m_player->moveToThread(m_thread);
    m_thread->start();
    m_player->initVlc();

    connect(m_player, SIGNAL(playAllTime(int)), this, SLOT(onVlcMediaPlayerVount(int)));
    connect(m_player, SIGNAL(playCurrentTime(int)), this, SLOT(onVlcMediaPlayerTimeChange(int)));
    // 播放进度
    connect(ui.videoSeekSlider, &WCSlider::sliderReleasedAt, [=](int currentPrecent) {
        if (m_player)
        {
            m_player->SetPlayTime(currentPrecent * 1000);
        }
    });
    // 音量进度
    connect(ui.videoVolumnSlider, &WCSlider::sliderReleasedAt, [=](int currentPrecent) {
        if (m_player)
        {
            m_player->SetVolume(currentPrecent);
        }
    });
    // 加载视频
    /*QTimer::singleShot(1000, [=]() {
        QString url = "https://vod.pipi.cn/43903a81vodtransgzp1251246104/bbd4f07a5285890808066187974/v.f42906.mp4";
        m_player->PlayUrl(url, (void*)ui.videoWidget->winId());
    });*/
}

void ClassVideoWidget::updateTime(int currentTime)
{
    QString currentMin = currentTime / 60 < 10 ? QString("0%1").arg(currentTime / 60) : QString::number(currentTime / 60);
    QString currentSecond = currentTime % 60 < 10 ? QString("0%1").arg(currentTime % 60) : QString::number(currentTime % 60);
    QString totalMin = m_totalTime / 60 < 10 ? QString("0%1").arg(m_totalTime / 60) : QString::number(m_totalTime / 60);
    QString totalSecond = m_totalTime % 60 < 10 ? QString("0%1").arg(m_totalTime % 60) : QString::number(m_totalTime % 60);
    ui.videoTime->setText(QString("%1:%2/%3:%4").arg(currentMin).arg(currentSecond).arg(totalMin).arg(totalSecond));
    // 刷新进度条
    ui.videoSeekSlider->setValue(currentTime);

    if (currentTime == m_totalTime)
    {
        // 播放结束
        m_isFinishPlay = true;
        ui.videoPause->setChecked(true);
    }
}

int ClassVideoWidget::volumn()
{
    return m_player->GetVolume();
}

int ClassVideoWidget::playTime()
{
    if (m_player)
    {
        return m_player->GetPlayTime();
    }
    return 0;
}

bool ClassVideoWidget::isPlaying()
{
    return m_player->IsPlaying();
}

bool ClassVideoWidget::isFinishPlaying()
{
    return m_player->IsEndPlaying();
}

void ClassVideoWidget::onVlcMediaPlayerVount(int count)
{
    // 设置视频当前音量
    m_lastVolumn = 100;
    ui.videoVolumnSlider->setValue(m_lastVolumn);
    // 保存视频的总长度
    m_totalTime = m_player->GetTime() / 1000;
    ui.videoSeekSlider->setMaximum(m_totalTime);
    updateTime(0);
    // 默认视频不自动播放，需要用户点击
    m_player->Pause();
}

void ClassVideoWidget::onVlcMediaPlayerTimeChange(int currentTime)
{
    updateTime(currentTime);
}

void ClassVideoWidget::onVlcMediaPlayerEnd()
{
}

void ClassVideoWidget::on_videoPause_clicked()
{
    if (ui.videoPause->isChecked())
    {
        m_player->Pause();
    }
    else
    {
        if (m_isFinishPlay)
        {
            m_player->StopPlaying();
            m_isFinishPlay = false;
        }
        m_player->Play();
    }
}

void ClassVideoWidget::on_videoVolumn_clicked()
{
    if (ui.videoVolumn->isChecked())
    {
        ui.videoVolumnSlider->setValue(0);
        // 设置视频当前音量
        m_lastVolumn = m_player->GetVolume();
        m_player->SetVolume(0);
    }
    else
    {
        // 设置视频当前音量
        ui.videoVolumnSlider->setValue(m_lastVolumn);
        m_player->SetVolume(m_lastVolumn);
    }
}

void ClassVideoWidget::on_videoClose_clicked()
{
    if (m_player)
    {
        m_player->Stop();
    }
    close();
}

void ClassVideoWidget::on_open_clicked()
{
    QString url = QFileDialog::getOpenFileName(NULL,QStringLiteral("打开视频"),"./","");
    if(!url.isEmpty()){
        m_player->PlayUrl("file:///"+url, (void*)ui.videoWidget->winId());
    }
}
